package com.example.miglayoutdemo;

import net.miginfocom.swing.MigLayout;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.swing.*;
import java.awt.*;

@SpringBootApplication
public class MigLayoutDemoApplication extends JFrame {

    public MigLayoutDemoApplication() {
        super();

        this.initFrame();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(600, 400));
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void initFrame() {
        JLabel firstNameLabel = new JLabel("First Name");
        final JTextField firstNameField = new JTextField();

        JLabel lastNameLabel = new JLabel("Surname");
        final JTextField lastNameField = new JTextField();

        JLabel addressLabel = new JLabel("Address");
        final JTextField addressField = new JTextField();

        JButton visibleButton = new JButton("显示/隐藏");
        visibleButton.addActionListener((e) -> {
            final boolean visible = addressLabel.isVisible();
            addressLabel.setVisible(!visible);
        });

        JPanel panel = new JPanel();
        final MigLayout layout = new MigLayout("wrap 4,filly,debug",
                "[align right,grow,growprio 50]20[grow,growprio 100,fill]30[grow]10:push[grow,fill]",
                "[grow 0]10:20:30[grow 0][]");
        panel.setLayout(layout);
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.add(firstNameLabel);
        panel.add(firstNameField);
        panel.add(lastNameLabel);
        panel.add(lastNameField);
        panel.add(addressLabel, "gapright push");
        panel.add(addressField, "span 2,wrap");
        panel.add(visibleButton, "");
        setContentPane(panel);
    }

    public static void main(String[] args) {
        SpringApplication.run(MigLayoutDemoApplication.class, args);
    }

}
